package com.cskaoyan.util;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class ValidationUtils {

    public static BaseRespVo dealWithFieldError(BindingResult bindingResult){

        if(bindingResult.hasFieldErrors()){

            FieldError fieldError = bindingResult.getFieldError();
            //获得成员变量名 → 请求参数名
            String field = fieldError.getField();
            //拒绝值 rejected Value
            Object rejectedValue = fieldError.getRejectedValue();
            //默认的message → 校验注解的message属性对应的值(自己写了message会用自己的)
            String message = fieldError.getDefaultMessage();
            StringBuffer stringBuffer = new StringBuffer();
            String messageBuffer = stringBuffer.append(message).toString();
//            String messageBuffer = stringBuffer.append("请求参数")
//                    .append(field)
//                    .append("因为传入了")
//                    .append(rejectedValue)
//                    .append("没有通过校验;")
//                    .append("message:")
//                    .append(message)
//                    .toString();
            return BaseRespVo.fail(messageBuffer);
        }
        return BaseRespVo.ok();
    }
}
